package com.imucs.campus_teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imucs.campus_teacher.entity.Teacher;
import com.imucs.campus_teacher.service.TeacherService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-09-27 11:41 PM
 */

@RestController
@CrossOrigin
@RequestMapping("/teacher")
public class TeacherController {


    @Autowired
    private TeacherService teacherService;


    @GetMapping("/test")
    public Result test(){
//        String test= (String) m.get("test1");


        return new Result(true, StatusCode.OK,"添加成功", teacherService.list());
    }

    @PostMapping("/login")
    public Result login(@RequestBody Map m ){
        Integer teacherId = (Integer) m.get("teacher_id");
        String password = (String) m.get("password");
        String username = (String) m.get("username");
        System.out.println(username);
        teacherId=Integer.valueOf(username);

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",teacherId);
        Teacher teacher =teacherService.getOne(queryWrapper);
        if (teacher!=null){
            if (teacher.getPassword().equals(password)){
                return new Result(true,StatusCode.OK,"成功",teacher);
            }else{
                return new Result(false,StatusCode.ERROR,"密码错误");
            }

        }else{
            return new Result(false,StatusCode.ERROR,"用户名不存在");
        }
    }

    @GetMapping("/list")
    public Result list(){

        return new Result(true,StatusCode.OK,"查询成功",teacherService.list());
    }

    @PostMapping("/selectBy")
    public Result selectBy(@RequestBody Map m){

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();


        if (null!=(Integer) m.get("teacher_id")){
            Integer teacherId=(Integer) m.get("teacher_id");
            queryWrapper.eq("teacher_id",teacherId);
        }
        if (null!=(String) m.get("realname")){
            String realname=(String) m.get("realname");
            queryWrapper.eq("realname",realname);
        }
        if (null!=(Integer) m.get("mobile")){
            Integer mobile=(Integer) m.get("mobile");
            queryWrapper.eq("mobile",mobile);
        }
        if (null!=(String) m.get("email")){
            String email=(String) m.get("email");
            queryWrapper.eq("email",email);
        }
        if (null!=(String) m.get("status")){
            String status=(String) m.get("status");
            queryWrapper.eq("status",status);
        }

        List<Teacher> teacherlist = teacherService.list(queryWrapper);
        if (0!=teacherlist.size()){
            return new Result(true,StatusCode.OK,"查询成功",teacherlist);
        }else{
            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }

    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody Map m){
        String id = (String) m.get("id");
        if (teacherService.removeById(id)){
            return new Result(true,StatusCode.OK,"删除成功");
        }else{
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Map m){

        Integer teacherId=(Integer) m.get("teacher_id");
        String password =(String) m.get("password");

        Teacher teacher = new Teacher();
        teacher.setTeacherId(teacherId);
        teacher.setPassword(password);
        if (null!=(String)m.get("realname")){
            teacher.setRealname((String) m.get("realname"));
        }
        if (null!=(Integer) m.get("mobile")){
            teacher.setMobile((Integer) m.get("mobile"));
        }

        if (null!=(String) m.get("email")){
            teacher.setEmail((String) m.get("email"));
        }

        teacher.setId(new IdWorker().nextId()+"");

        teacher.setCreateDate(new Date());

        teacher.setStatus("1");

        if (teacherService.save(teacher)) {
            return new Result(true,StatusCode.OK,"添加成功");
        }else {
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody Map m){

        String id = (String) m.get("id");

        Teacher teacher =teacherService.getById(id);
        if (null!=teacher){
            if (null!=(Integer)m.get("teacher_id")){

                teacher.setTeacherId((Integer)m.get("teacher_id"));
            }
            if (null!=(String)m.get("password")){

                teacher.setPassword((String)m.get("password"));
            }
            if (null!=(String)m.get("realname")){

                teacher.setRealname((String)m.get("realname"));
            }
            if (null!=(Integer)m.get("mobile")){

                teacher.setMobile((Integer) m.get("mobile"));
            }
            if (null!=(String)m.get("status")){

                teacher.setStatus((String) m.get("status"));
            }
            if (null!=(String)m.get("email")){

                teacher.setEmail((String) m.get("email"));
            }

            if (teacherService.saveOrUpdate(teacher)) {
                return new Result(true,StatusCode.OK,"修改成功");
            }else{
                return new Result(false,StatusCode.ERROR,"修改失败");
            }

        }else{
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }
}
